using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SysCommon.Entity.Business;

namespace CVSS.Client.SC.Tools
{
    static class PositionCacheHelper
    {
        static Dictionary<string, LatestGPS_Ety> _cache =
            new Dictionary<string,LatestGPS_Ety>(1000);

        public static void Insert(LatestGPS_Ety ety)
        {
            try
            {
                if (ety == null)
                    return;

                lock (_cache)
                {
                    string carplate = ety.CARPLATE;
                    if (string.IsNullOrEmpty(carplate))
                        return;

                    if (_cache.ContainsKey(carplate))
                        _cache[carplate] = ety;
                    else
                        _cache.Add(carplate, ety);
                }
            }
            catch (Exception ex)
            {
                SysCommon.Tools.AutoLogHelper.Error(ex);
            }
        }

        public static void Remove(string carplate)
        {
            try
            {
                lock (_cache)
                {
                    if (string.IsNullOrEmpty(carplate))
                        return;

                    if (_cache.ContainsKey(carplate))
                        _cache.Remove(carplate);
                }
            }
            catch (Exception ex)
            {
                SysCommon.Tools.AutoLogHelper.Error(ex);
            }
        }

        public static void Reset()
        {
            try
            {
                lock (_cache)
                {
                    _cache.Clear();
                }
            }
            catch (Exception ex)
            {
                SysCommon.Tools.AutoLogHelper.Error(ex);
            }
        }

        public static void Reset(IEnumerable<LatestGPS_Ety> etys)
        {
            Reset();
            Insert(etys);
        }

        public static void Insert(IEnumerable<LatestGPS_Ety> etys)
        {
            try
            {
                lock (_cache)
                {
                    if (etys != null)
                    {
                        foreach (LatestGPS_Ety ety in etys)
                            Insert(ety);
                    }
                }
            }
            catch (Exception ex)
            {
                SysCommon.Tools.AutoLogHelper.Error(ex);
            }
        }

        public static IEnumerable<LatestGPS_Ety> Search(IEnumerable<string> carplates)
        {
            if (carplates == null)
                return null;

            LatestGPS_Ety ety;
            List<LatestGPS_Ety> etyList = new List<LatestGPS_Ety>();
            foreach (string carplate in carplates)
            {
                ety = Search(carplate);
                if (ety != null)
                    etyList.Add(ety);
            }

            return etyList;
        }

        public static LatestGPS_Ety Search(string carplate)
        {
            try
            {
                LatestGPS_Ety ety;
                if (_cache.TryGetValue(carplate, out ety))
                    return ety;
            }
            catch (Exception ex)
            {
                SysCommon.Tools.AutoLogHelper.Error(ex);
            }

            return null;
        }
    }
}